using System.Drawing;

namespace GDI
{
    public class RectAngle : PrintableItem
    {
        /* -------------------------------------------------------------------------------------------- */

        private RectangleF f_Rectangle;
        private Pen f_LinePen;

        private readonly SolidBrush f_FillBrush = new SolidBrush(Color.Transparent);

        /* -------------------------------------------------------------------------------------------- */

        public Pen LinePen
        {
            get { return f_LinePen; }
            set { f_LinePen = value; OnPropertyChanged(); }
        }

        public Color FillColor
        {
            get { return f_FillBrush.Color; }
            set { f_FillBrush.Color = value; OnPropertyChanged(); }
        }

        public RectangleF LineRectangle { get { return f_Rectangle; } set { f_Rectangle = value; OnPropertyChanged(); } }

        public PointF LeftUp
        {
            get { return f_Rectangle.Location; }
            set { f_Rectangle.Location = value; OnPropertyChanged(); }
        }

        public PointF RigthDown
        {
            get { return f_Rectangle.Location.Add(f_Rectangle.Size); }
            set
            {
                f_Rectangle.Width = value.X - f_Rectangle.X;
                f_Rectangle.Height = value.Y - f_Rectangle.Y;
                OnPropertyChanged();
            }
        }

        public PointF Center
        {
            get
            {
                return new PointF(f_Rectangle.X + f_Rectangle.Width / 2, f_Rectangle.Y + f_Rectangle.Height / 2);
            }
            set
            {
                var lv_NewCenter = value;
                var lv_OldCenter = Center;
                var lv_Delta = lv_NewCenter.Substract(lv_OldCenter);
                f_Rectangle.Location = f_Rectangle.Location.Add(lv_Delta);
                OnPropertyChanged();
            }
        }

        public SizeF Size
        {
            get { return f_Rectangle.Size; }
            set
            {
                var lv_NewSize = value;
                var lv_OldSize = Size;
                var lv_Delta = lv_NewSize.Substract(lv_OldSize);
                lv_Delta = lv_Delta.Devide(2);
                f_Rectangle.Location = f_Rectangle.Location.Substract(lv_Delta);
                f_Rectangle.Size = lv_NewSize;
                OnPropertyChanged();
            }
        }

        /* -------------------------------------------------------------------------------------------- */


        /* -------------------------------------------------------------------------------------------- */

        protected override void Draw(Graphics g)
        {
            var lv_Region = Rectangle.Round(f_Rectangle);
            g.DrawRectangle(f_LinePen, lv_Region);
            if(f_FillBrush.Color != Color.Transparent)
                g.FillRectangle(f_FillBrush, lv_Region);

        }

        /* -------------------------------------------------------------------------------------------- */


        /* -------------------------------------------------------------------------------------------- */
    }
}